<template>
  <div class="role-content-box">
    <!-- 表格操作 -->
    <div class="table—operate">
        <el-button type="primary" plain @click="synchronousData">数据同步</el-button>
    </div>

    <!-- 表格内容 -->
    <el-table :data="viewData.dataList" style="width: 100%;margin:20px 0px" border  row-key="deptId">
        <el-table-column prop="deptName" label="部门名称">
        <template #default="{row}">
            {{row.deptName}}
          <el-link type="primary" style="float:right" @click="lookUserListHandle(row)">查看成员</el-link>
          <el-link type="primary" style="float:right;margin-right:20px" @click="synchronousUserData(row)">同步用户</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="directorUserName" label="主管" width="120" />
      <el-table-column label="设置部门助理" width="120">
		<template #default="{row}">
			<el-link type="primary" @click="editHandle(row)">设置</el-link>
		</template>
      </el-table-column>
      <el-table-column prop="dataScope" label="数据范围"  width="150">
        <template #default="{row}">
          {{ $filters.translate('dataScopeStatus', row.dataScope)}}
        </template>
      </el-table-column>
    </el-table>


    <!-- 新增修改弹框 -->
    <addEdit :childConfig='viewData.childConfig' :formData="viewData.formData" @upDrawerStatus="upDrawerStatusHandle" @upViewList="getViewTableList" />
  </div>
</template>


<script setup>
import {reactive} from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'

import { getDeptUser, getDingDept } from '^/organization/department.js'

import addEdit from './addEdit.vue'

const store = useStore()
const router = useRouter()

// header 搜索
const viewData = reactive({
    formData: {}, //用于修改
    childConfig: { 
        title: '新增角色',
        DrawerStatus: false, //弹框状态
        confirmFunctionName: '' //执行确认按钮框调用的函数
    },
    dataList: [] //表格列表
})
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
 * 接口操作
 */
// 同步数据
const synchronousData = () => {
    getDingDept().then(res => {
        console.log("同步钉钉部门同步", res.data.data, res.data.message)
        getViewTableList()
    }, err => {
        console.log("同步钉钉部门异常", err)
    })
}
// 同步用户数据
const synchronousUserData = ({deptId}) => {
    getDeptUser(deptId).then(res => {
        console.log("钉钉部门用户同步", res.data.data, res.data.message)
        getViewTableList()
    }, err => {
        console.log("钉钉部门用户同步异常", err)
    })
}
// 获取表格数据列表
const getViewTableList = () => {
    store.dispatch('getDeptList').then(res => {
        viewData.dataList = res.data
        viewData.total = res.total
    }, err => {
        console.log("钉钉部门异常", err)
    })
}
getViewTableList()

// 查看成员
const lookUserListHandle = ({deptId}) => {
    router.push({ name: 'userManage', params: { deptId } })
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

// 新增弹框对父组件处理
const upDrawerStatusHandle = (val) => {
    viewData.childConfig.DrawerStatus = val
}

// 修改
const editHandle = ({deptId}) => {
    viewData.childConfig = { 
        title: '设置部门助理',
        DrawerStatus: true, //弹框状态
        confirmFunctionName: 'editHandleApi' //执行确认按钮框调用的函数
    }
    console.log(deptId)
    viewData.formData = {deptId}
}
</script>